package com.yaoxiaowen.utils;



import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 处理时间的工具
 *
 * @author: huangyue on 2016/10/27 9:49.
 * @email: hy42744@ly.com
 */
@SuppressWarnings("unused")
public class Utils_Time {
    public static final SimpleDateFormat YYYY_MM_DD_HH_mm_ss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
    public static final SimpleDateFormat YYYY_MM_DD_HH_mm = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
    public static final SimpleDateFormat MM_DD_HH_mm = new SimpleDateFormat("MM月dd号 HH:mm", Locale.CHINA);
    public static final SimpleDateFormat YYYY_MM_DD = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
    public static final SimpleDateFormat YYYYMMDD = new SimpleDateFormat("yyyyMMdd", Locale.CHINA);
    public static final SimpleDateFormat MM_DD_cn = new SimpleDateFormat("MM月dd日", Locale.CHINA);
    public static final SimpleDateFormat HH_MM = new SimpleDateFormat("HH:mm", Locale.CHINA);
    public static final SimpleDateFormat MM_DD_CHINA = new SimpleDateFormat("MM月dd号", Locale.CHINA);

    /**
     * 获取格式化日期（距离“2016-02-12”的increment天的日期）
     *
     * @param date
     * @param increment
     * @return
     */
    public static String getFormatDate(Date date, int increment) {
        return YYYY_MM_DD.format(getDate(date, increment));
    }

    public static String getFormatDate(Date date) {
        return MM_DD_cn.format(date);
    }

    public static Date getDate(String formatDate) {
        Date date = new Date();
        try {
            date = YYYYMMDD.parse(formatDate.replaceAll("-",""));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 获取date（距离“2016-02-12”的increment天的date）
     *
     * @param date
     * @param increment
     * @return
     */
    public static Date getDate(Date date, int increment) {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_YEAR, increment);
        return cal.getTime();
    }

    public static Date getDateFrom_HM_Format(String hh_mm) {
        Date date = null;
        try {
            date = HH_MM.parse(hh_mm);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String getHMS(long millisecond) {
        StringBuilder result = new StringBuilder();
        long second = millisecond / 1000;
        long minute = 0;
        long hour = 0;
        if (second > 60) {
            minute = second / 60;
            second = second % 60;
        }
        if (minute > 60) {
            hour = minute / 60;
            minute = minute % 60;
        }

        if (hour >= 0 && hour < 10)
            result.append("0").append(hour);
        else {
            result.append(hour);
        }
        result.append(":");
        if (minute >= 0 && minute < 10)
            result.append("0").append(minute);
        else {
            result.append(minute);
        }
        result.append(":");
        if (second >= 0 && second < 10)
            result.append("0").append(second);
        else {
            result.append(second);
        }
        return result.toString();
    }

    /**
     * 获取时间格式：xx天xx小时xx分钟
     *
     * @param minute
     * @return
     */
    public static String getDHM(long minute) {
        StringBuilder result = new StringBuilder();
        long hour = 0;
        long day = 0;
        if (minute > 60) {
            hour = minute / 60;
            minute = minute % 60;
        }
        if (hour >= 24) {
            day = hour / 24;
            hour = hour % 24;
        }

        if (day > 0)
            result.append(day).append("天");
        if (hour > 0)
            result.append(hour).append("小时");
        if (minute > 0)
            result.append(minute).append("分钟");
        return result.toString();
    }

    /**
     * 将2016-11-11 10:20:55转成中文“11月11号 10:20”的时间格式
     *
     * @param time
     * @return
     */
    public static String getCN_Format(String time) {
        Date date = new Date();
        try {
            date = YYYY_MM_DD_HH_mm_ss.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return MM_DD_HH_mm.format(date);
    }

    /**
     * 转换时间格式
     * @param time   2016-10-29 14:30:00
     * @return  10月29号
     */
    public static String getChineseMonthDayFormat(String time){
        Date date = new Date();
        try {
            date = YYYY_MM_DD_HH_mm_ss.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return MM_DD_CHINA.format(date);
    }

    /**
     *   转换时间格式
     * @param time  2016-10-29 14:30:00
     * @return  14:30
     */
    public static String getHHMMFormat(String time){
        try {
            return HH_MM.format(YYYY_MM_DD_HH_mm_ss.parse(time));
        } catch (ParseException e) {
            return "";
        }
    }


    /**
     *   转换时间格式
     * @param time  2016-10-29 14:30
     * @return  14:30
     */
    public static String getHHMMFromYMDHM(String time){
        Date date = new Date();
        try {
            date = YYYY_MM_DD_HH_mm.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return HH_MM.format(date);
    }

   



    /**
     * 比较日期大小
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isBefore(Date date1,Date date2) {
        return date1.getTime() < date2.getTime();
    }

    /**
     * 将格式化的时间，转化为时间戳
     * @param formatStr   2016-12-26 08:57:00
     * @return 时间戳
     */
    public static long formatTime2TimeStamp(String formatStr){
        SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date;
        try {
            date = format.parse(formatStr);
            return date.getTime();
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 将包含格式化日期时间的字符串。转化为Date进行返回
     * @param formatStr 2016-11-08 14:00:00
     * @return  Date
     */
    public static  Date String2Date(String formatStr){
        Date date = new Date();
        try {
            date = YYYY_MM_DD_HH_mm_ss.parse(formatStr);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 判断当前日期是否属于夜间。
     * 判断依据：系统时间0:00~06:00，23：00~24:00
     * @return
     */
    public static boolean isNight(){
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);

        if (hour<6 || hour>=23){
            return true;
        }

        return false;
    }

    /**
     * 计算跨的天数（仅限跨一个月计算）
     * @param before
     * @param after
     * @return
     */
    public static int minusDay(String before,String after){
        int minusDays = 0;
        try {
            Date beforeDate = YYYY_MM_DD.parse(before);
            Date afterDate = YYYY_MM_DD.parse(after);
            int beforeDay = beforeDate.getDate();
            int afterDay = afterDate.getDate();
            minusDays = afterDate.getDate() - beforeDate.getDate();
            if (minusDays < 0){
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(beforeDate);
                int sumDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
                minusDays = sumDays - beforeDay;
                minusDays += afterDay;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return minusDays;
    }

}
